package com.hqyj.ssm_project.service;

import com.hqyj.ssm_project.exception.ServiceException;
import com.hqyj.ssm_project.mapper.UserMapper;
import com.hqyj.ssm_project.pojo.User;
import com.hqyj.ssm_project.utils.PageUtil;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
@Service
public class UserServiceImp implements UserService{
    //获取日志对象
    private Logger logger = LoggerFactory.getLogger(UserServiceImp.class);
    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean addUser(User user) throws ServiceException {
        logger.debug("UserServiceImp>>addUser start...");

        boolean result= false;
        try {
//            int a=1/0;
            result = false;
            int num = userMapper.insertSelective(user);
            if(num>0){
                result=true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getLocalizedMessage());
            throw new ServiceException("服务层发成异常",e);
        }
        logger.debug("UserServiceImp>>addUser end...");
        return result;
    }
    @Override
    public List<User> findAllUser(){
        logger.debug("UserServiceImp>>findAllUser start...");
        List<User> users = userMapper.selectUser();
        logger.debug("UserServiceImp>>findAllUser end...");
        return users;
    }

    @Override
    public User selectByPrimaryKey(Integer userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    @Override
    public int updateByPrimaryKeySelective(User row) {
        return userMapper.updateByPrimaryKeySelective(row);
    }

    @Override
    public int deleteByPrimaryKey(Integer userId) {
        return userMapper.deleteByPrimaryKey(userId);
    }

    @Override
    public List<User> findUserNoidcard() {
        return userMapper.findUserNoidcard();
    }

    @Override
    public void updateIdcardByUser(Integer userId) {
        userMapper.updateIdcardByUser(userId);
    }

    @Override
    public List<User> selectUserAndIdcardAndDetp() {
        return userMapper.selectUserAndIdcardAndDetp();
    }

    @Override
    public void updateUserByDeptId(Integer id) {
        userMapper.updateUserByDeptId(id);
    }

    @Override
    public User selectUserAndDetp(Integer userId) {
        return userMapper.selectUserAndDetp(userId);
    }

    @Override
    public User login(User user) {
        return userMapper.login(user);
    }

    @Override
    public PageUtil<User> findUserByPageNumber(int currentNum){
        logger.debug("UserServiceImp>>findUserByPageNumber start……");
        PageUtil<User> userPageUtil = this.userMapper.selectTotalRecordNumber();
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("startindex",(currentNum-1)*userPageUtil.getPageUnit());
//        map.put("pageunit",userPageUtil.getPageUnit());
        //List<User> userList = this.userMapper.selectUserByPageNum(map);
        Integer startindex=(currentNum-1)*userPageUtil.getPageUnit();
        Integer pageunit=userPageUtil.getPageUnit();
        List<User> userList = userMapper.selectUserByPageN(startindex, pageunit);
        //List<User> userList = this.userMapper.selectUserByPageNu();
        //List<User> userList = this.userMapper.selectUserAndIdcardAndDetp();
        userPageUtil.setDataList(userList);
        userPageUtil.setCurrentNum(currentNum);

        logger.debug("UserServiceImp>>findUserByPageNumber end……");
        return userPageUtil;
    }

    @Override
    public List<User> selectSuperUser(Integer roleId) {
        return userMapper.selectSuperUser(roleId);
    }

    @Override
    public User findCardUser(String cardNum) {
        return userMapper.findCardUser(cardNum);
    }
}
